package com.guanlixitong.controller.impl;

import com.guanlixitong.controller.IUserController;
import com.guanlixitong.factory.Factory;
import com.guanlixitong.service.IUserService;
import com.guanlixitong.wenjian.util.EmailUtil;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Random;
import java.util.Scanner;

public class UserControllerImpl implements IUserController {

    private IUserService userService = Factory.getUserService();

    private Scanner sc = new Scanner(System.in);

    //注册信息
    public void ZhuCe() throws IOException {

        System.out.println("请输入用户名：");
        String username = sc.next();
        System.out.println("请输入密码：");
        String password = sc.next();
        System.out.println("请输入手机号：");
        String phone = sc.next();
        String s = yanzhengma();
        userService.ZhuCePanDuan(s, username, password, phone);
    }

    //验证码
    private String yanzhengma() {

        System.out.println("请输入您的邮箱");
        String mailNum = sc.next();
        StringBuilder sb = new StringBuilder();
        Random r = new Random();
        for (int i = 0; i < 6; i++) {
            sb.append(r.nextInt(10));
        }
        String s = sb.toString();

        EmailUtil.sendMail(mailNum,s,"验证码");
        System.out.println("验证码已发至邮箱");
        return s;

    }

    //修改密码
    @Override
    public void xiugaimima() throws IOException {
        Scanner sc = new Scanner(System.in);
        String s = yanzhengma();
        BufferedReader fw = new BufferedReader(new FileReader("D:\\yonghu.txt", StandardCharsets.UTF_8));
        String b;
        if ((b = fw.readLine()) == null) {
            System.out.println("请注册信息");
            return;
        }
        System.out.println("请输入手机号：");
        String number = sc.next();
        String s1;
        BufferedReader fw1 = new BufferedReader(new FileReader("D:\\yonghu.txt", StandardCharsets.UTF_8));
        userService.XiuGaiMiMa(sc, s, number, fw1);
    }

    //登录
    @Override
    public boolean Login() throws IOException, InterruptedException {
        Scanner sc = new Scanner(System.in);
        int count = 1;
        boolean flag = userService.loginpanduan(sc, count);
        return flag;
    }

}
